import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/expired",
    component: () => import("@/views/expired/index.vue"),
    meta: {
      hidden: true
    }
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    component: Layouts,
    redirect: "/infusion-configuration",
    name: "DeviceType",
    meta: {
      title: "智慧输液配置",
      svgIcon: "infusion",
      alwaysShow: true,
      id: "",
      roles: ["admin"] // 可以在根路由中设置角色
    },
    children: [
      {
        path: "infusion-configuration",
        component: () => import("@/views/menu/infusion-configuration/index.vue"),
        name: "Device",
        meta: {
          title: "设备管理",
          id: "",
          roles: ["admin"] // 可以在根路由中设置角色
        }
      }
    ]
  },
  {
    path: "/information-records",
    component: Layouts,
    redirect: "/information-records/index",
    name: "Record",
    meta: {
      title: "信息记录",
      svgIcon: "information",
      alwaysShow: true,
      id: "",
      roles: ["admin"] // 可以在根路由中设置角色
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/menu/information-records/index.vue"),
        name: "RecordOperate",
        meta: {
          title: "操作日志",
          keepAlive: true,
          id: "",
          roles: ["admin"] // 可以在根路由中设置角色
        }
      }
    ]
  },

  {
    path: "/architecture-management",
    component: Layouts,
    redirect: "/architecture-management/index",
    name: "Area",
    meta: {
      title: "架构管理",
      svgIcon: "framework",
      alwaysShow: true,
      id: "",
      roles: ["admin"] // 可以在根路由中设置角色
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/menu/architecture-management/index.vue"),
        name: "AreaReceiver",
        meta: {
          title: "分区管理",
          id: "",
          roles: ["admin"] // 可以在根路由中设置角色
        }
      }
    ]
  },
  {
    path: "/system-settings",
    component: Layouts,
    redirect: "/system-settings/user",
    name: "System",
    meta: {
      title: "系统设置",
      svgIcon: "system",
      roles: ["admin"], // 可以在根路由中设置角色
      alwaysShow: true, // 将始终显示根菜单
      id: ""
    },
    children: [
      {
        path: "user",
        component: () => import("@/views/menu/system-settings/user/index.vue"),
        name: "DoctorsUser",
        meta: {
          title: "后台用户配置",
          roles: ["admin"], // 或者在子导航中设置角色
          id: ""
        }
      },
      {
        path: "role",
        component: () => import("@/views/menu/system-settings/role/index.vue"),
        name: "DoctorsRole",
        meta: {
          id: "",
          roles: ["admin"], // 可以在根路由中设置角色
          title: "后台角色配置" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        console.info("111")
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
